#ifndef __FUSION_H__
#define __FUSION_H__

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include "queue.h"
#include "common.h"
#include "radar_protocol.h"
#include "video_protocol.h"

#define MAX_FUSION_DATA_SIZE      100
#define MAX_RELAY_QUEUE_SIZE      30
#define MAX_RELAY_SUCCESSFUL_SIZE 100
#define MAX_RELAY_DISTANCE        2000
#define MAX_POLE_RELAY_DISTANCE   7000

#define MAX_LANE_OBJ_NUM 50

enum {
    relay_none = 0,
    relay_successful = 1,
    relay_failed = 2,
    relay_once = 3,
};

enum {
    update_normal = 0,
    update_timeout = 1,
};

typedef struct {
    uint16_t radar_id;
    char video_guid[64];
} match_info_t;

typedef struct {
    match_info_t match_info;
    float similarity;
} fusion_data_t;

typedef struct {
    uint16_t id;
    int8_t origin_lane;
    int8_t current_lane;
    int64_t change_time;
} lane_info_t;

typedef struct {
    uint16_t relay_id;
    uint16_t origin_id;
    int64_t update_time;
    union {
        int relay_status;
        int update_status;
    };
    int16_t first_occurrence_pos_x;
    int16_t first_occurrence_pos_y;
    int32_t dis_diff;
    radar_obj_info_t radar_obj_info;
    struct_info_t video_obj_info;
} relay_t;

typedef relay_t leave_t;

typedef struct {
    Queue_t *in_fusion_data_queue;          // 来向融合数据队列（来自来向雷达和视频数据融合结果）
    Queue_t *out_fusion_data_queue;         // 去向融合数据队列（来自去向雷达和视频数据融合结果）

    Queue_t *relay_queue[MAX_ROWS];         // 每个车道的盲区接力队列
    Queue_t *relay_successful_queue;        // 杆内接力成功目标队列

    Queue_t *new_radars_queue;              // 新雷达目标队列（包含刚进入检测范围的目标）

    Queue_t *pole_relay_queue;              // 杆杆接力候选队列
    Queue_t *pole_relay_successful_queue;   // 杆杆接力成功队列

    Queue_t *third_relay_successful_queue;  // 第三方数据接力成功队列
} fusion_manager_t;

void append_relays_queue(radar_obj_info_t *radar_obj_info);
void append_pole_relays_queue(relay_t *relay);
void try_to_relay(radar_frame_t *radar_frame);
void try_to_pole_relay(radar_frame_t *radar_frame);
void try_to_relay_by_third(radar_frame_t *radar_frame);
relay_t *get_successful_relay_data(uint16_t radar_id);
relay_t *get_successful_pole_relay_data(uint16_t radar_id);
relay_t *get_successful_third_relay_data(uint16_t radar_id);
char *get_video_by_radar(uint16_t radar_id, uint8_t dev_type);
uint16_t get_radar_by_video(char *video_guid, uint8_t dev_type);
const char *get_car_type(bool is_mix, uint16_t type);
const char *get_color_by_code(uint16_t code);
void start_fusion_server(void);
void stop_fusion_server(void);
#endif  // !__FUSION_H__